#include "../src/HMAC_MD5.c"

const unsigned int k[16] = {
    0x0b0b0b0b, 0x0b0b0b0b, 0x0b0b0b0b, 0x0b0b0b0b,
    0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x00000000, 0x00000000, 0x00000000, 0x00000000
};

void readFile(char *path, char *a) {
    FILE *fp;
    if ((fp = fopen(path, "r")) == NULL) {
        perror("fail to read");
        exit(1);
    }

    char buf[1024] = { '\0' };
    int len;

    while (fgets(buf, 1024, fp) != NULL) {
        len = strlen(buf);
        if (buf[len-1] == '\n') {
            strncat(a, buf, len-1);
        } else {
            strcat(a, buf);
        }
    }
}

void test() {
    // const char *a = "123456789123sdfsdfdfd";

    char a[maxLen];
    readFile("input.txt", a);
    printf("input: %s\n", a);
    int num = 0;
    unsigned long long int inputLen = 0;
    unsigned int block[1024][16];
    append(a, block, &num, &inputLen);

    unsigned int CV_in[4];
    CV_in[0] = A;
    CV_in[1] = B;
    CV_in[2] = C;
    CV_in[3] = D;

    int result1[4];
    int result2[128];
    
    MD5(CV_in, block, &num, result1);
    transform(result1, 4, result2);
    printf("MD5result:  ");
    for (int i = 0; i < 128; ++i) {
        printf("%0x", result2[i]);
    }
    printf("\n");

    int result[128];
    HMAC(CV_in, k, block, num, inputLen, result);


    printf("HMACresult: ");
    for (int i = 0; i < 128; ++i) {
        printf("%d", result[i]);
    }
    printf("\n");
}

void test_MD5() {
    const char *a = "abc";
    printf("input: %s\n", a);
    int num = 0;
    unsigned long long int inputLen = 0;
    unsigned int block[1024][16];
    append(a, block, &num, &inputLen);

    unsigned int CV_in[4];
    CV_in[0] = A;
    CV_in[1] = B;
    CV_in[2] = C;
    CV_in[3] = D;

    int result1[4];
    int result2[128];
    
    MD5(CV_in, block, &num, result1);
    transform(result1, 4, result2);
    printf("MD5result:  ");
    for (int i = 0; i < 128; ++i) {
        printf("%0x", result2[i]);
        if ((i+1) % 4 == 0) {
            printf(" ");
        }
    }
    printf("\n");

    const char *a1 = "a";
    printf("input: %s\n", a1);
    int num1 = 0;
    unsigned long long int inputLen1 = 0;
    unsigned int block1[1024][16];
    append(a1, block1, &num1, &inputLen1);

    unsigned int CV_in1[4];
    CV_in1[0] = A;
    CV_in1[1] = B;
    CV_in1[2] = C;
    CV_in1[3] = D;

    int result11[4];
    int result21[128];
    
    MD5(CV_in1, block1, &num1, result11);
    transform(result11, 4, result21);
    printf("MD5result:  ");
    for (int i = 0; i < 128; ++i) {
        printf("%0x", result21[i]);
        if ((i+1) % 4 == 0) {
            printf(" ");
        }
    }
    printf("\n");

    const char *a2 = "message digest";
    printf("input: %s\n", a2);
    int num2 = 0;
    unsigned long long int inputLen2 = 0;
    unsigned int block2[1024][16];
    append(a2, block2, &num2, &inputLen2);

    unsigned int CV_in2[4];
    CV_in2[0] = A;
    CV_in2[1] = B;
    CV_in2[2] = C;
    CV_in2[3] = D;

    int result12[4];
    int result22[128];
    
    MD5(CV_in2, block2, &num2, result12);
    transform(result12, 4, result22);
    printf("MD5result:  ");
    for (int i = 0; i < 128; ++i) {
        printf("%0x", result22[i]);
        if ((i+1) % 4 == 0) {
            printf(" ");
        }
    }
    printf("\n");
}

void test_HMAC() {
    const char *a = "Hi There";
    printf("input: %s\n", a);
    int num = 0;
    unsigned long long int inputLen = 0;
    unsigned int block[1024][16];
    append(a, block, &num, &inputLen);

    unsigned int CV_in[4];
    CV_in[0] = A;
    CV_in[1] = B;
    CV_in[2] = C;
    CV_in[3] = D;

    int result[128];
    HMAC(CV_in, k, block, num, inputLen, result);


    printf("HMACresult: ");
    for (int i = 0; i < 128; ++i) {
        printf("%d", result[i]);
        if ((i+1) % 4 == 0) {
            printf(" ");
        }
    }
    printf("\n");

    const unsigned int k1[16] = {
        0x4a656665, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000
    };

    const char *a1 = "what do ya want for nothing?";
    printf("input: %s\n", a1);
    int num1 = 0;
    unsigned long long int inputLen1 = 0;
    unsigned int block1[1024][16];
    append(a1, block1, &num1, &inputLen1);

    unsigned int CV_in1[4];
    CV_in1[0] = A;
    CV_in1[1] = B;
    CV_in1[2] = C;
    CV_in1[3] = D;

    int result1[128];
    HMAC(CV_in1, k1, block1, num1, inputLen1, result1);


    printf("HMACresult: ");
    for (int i = 0; i < 128; ++i) {
        printf("%d", result1[i]);
        if ((i+1) % 4 == 0) {
            printf(" ");
        }
    }
    printf("\n");
}


int main() {
    //test();
    //test_MD5();
    test_HMAC();
}